<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            border: 1px solid blue;
            border-collapse: collapse;
            width: 800px;
        }

        table tr th,
        table tr td {
            border: 1px solid blue;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        请输入学生信息:
        <input type="text" name="" id="" v-model="newname" placeholder="请输入姓名" />
        <input type="text" name="" id="" v-model="newage" placeholder="请输入年龄" />
        <input type="text" name="" id="" v-model="newtel" placeholder="请输入电话" />
        <button @click="addStu">添加</button>
        <table>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th @click="mySort">{{ages}}</th>
                <th>电话</th>
            </tr>
            <tr v-for="item in stu" :key="item.id">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td>{{item.tel}}</td>
            </tr>
        </table>
    </div>
    <script src="./js/vue.js"></script>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                newname: "",
                newage: "",
                newtel: "",
                num: 5,
                ages: '年龄',
                stu: [
                    { "id": 1, "name": "马可", "age": 6, "tel": "10086" },
                    { "id": 2, "name": "李白", "age": 50, "tel": "10010" },
                    { "id": 3, "name": "韩信", "age": 60, "tel": "10002" },
                    { "id": 4, "name": "后羿", "age": 300, "tel": "10050" },
                    { "id": 5, "name": "赵云", "age": 28, "tel": "10004" },
                ]
            },
            methods: {
                addStu() {
                    this.stu.push({
                        "id": this.num,
                        "name": this.newname,
                        "age": this.newage,
                        "tel": this.newtel
                    })
                    this.num++;
                    this.newname = '',
                        this.newtel = '',
                        this.newage = ''
                },


                mySort() {
                    if (k % 2 == 0) {
                        this.hero.sort((a, b) => a.age - b.age);
                        this.ages = '年龄↑'
                        k++
                    } else {
                        this.hero.sort((a, b) => b.age - a.age);
                        this.ages = '年龄↓';
                        k++;
                    }
                }

            }
        })
    </script>
</body>

</html>